<?php
class RoleModel extends CommonModel {
	private $role_id = '' ;
	private $role_cache_path = '' ;
	private $perm_data = array() ;
	public function __construct(){
		parent::__construct();
	}
/*
	 *@page 第几页
	 *@per_page 每一页显示的数据 
	 */
	public function queryRoleList($p =  1 ,$per_page , $where = array() ){
		$getwhere = " 1 = 1 " ;
		if(isset($where['rolename']) && $where['rolename']){
			$getwhere.=" and rolename like '%{$where['rolename']}%' ";
		}
		import('Library.Page' , APP_PATH);
		$data = array() ;
		$total = M("Admin_role")->where($getwhere)->count();;
		$page = new Page($total,$per_page);
		$page_string =  $page->show();
		if($p <=0 ){
			$p = 1 ;
		}
		$limit = ($p-1)*$per_page;
		$limit.=",{$per_page}";
		$list = M("Admin_role")->where($getwhere)->order('id desc ')->limit($limit)->select();
		//echo M("Admin_role")->getLastSql();
		foreach($list as $k=>$v){
			$list[$k]['status'] = ($v['status'] == 1 )?"开启":'<font color="red">关闭</font>';
			$list[$k]['cache_file'] = (file_exists(C("role_cache_path")."/cache_role_{$v['id']}.inc.php"))?"存在":'<font color="red">不存在</font>';
			$list[$k]['filemtime'] = (file_exists(C("role_cache_path")."/cache_role_{$v['id']}.inc.php"))?date("Y-m-d H:i:s",filemtime(C("role_cache_path")."/cache_role_{$v['id']}.inc.php")):'<font color="red">文件不存在</font>';
		}
		$data = array(
			'list'=>$list , 
			'page_sting'=>$page_string		
		);
		return $data ;
	}
	//添加角色
	public function addRole($data){
		$returnData = $this->queryRolenameExists($data['rolename']) ;
		if($returnData){
			return array('status'=>false , 'message'=>"角色名称{$data['rolename']}已经存在" ) ;
		}
		$id = M("Admin_role")->add($data);
		
		$sql = M("Admin_role")->getLastSql();
		if($id>=1){
			write_action_log($sql,getModuleAction(),$this->username,get_client_ip(),1,"添加角色为{$data['rolename']}成功");
			return array('status'=>true , 'message'=>'success' ) ;
		}else{
			write_action_log($sql,getModuleAction(),$this->username,get_client_ip(),0,"添加角色为{$data['rolename']}失败");
			return array('status'=>false , 'message'=>'服务器繁忙请稍后' ) ;
		}
	}
	//根据角色名称查询是不是存在
	public function queryRolenameExists($rolename){
		return M("Admin_role")->where(" rolename = '{$rolename}' ")->find();
		
	}
	//根据角色id查询角色是否存在
	public function queryRoleById($id){
		return M("Admin_role")->where(" id = '{$id}' ")->find();
		
	}
	//设置权限
	public function updateRole($data){
		$info  = $this->queryRoleById($data['id']);
		if(!$info){
			write_action_log("",getModuleAction(),$this->username,get_client_ip(),0,"参数错误");
			return array('status'=> 0 , 'message'=>'数据不存在' ) ;
		}
		$role_array = $data['role_array'];
		if($role_array){
			for($k = 0 ; $k<count($role_array);$k++){
				$role_data[] = $role_array[$k];
			}
			$perm = serialize($role_data);
		}
	
		$update_data = array(
				'rolename'=>$data['rolename'] ,
				'perm'=>$perm ,
				'status'=>$data['status']
		);
		$status = M("Admin_role")->where("id = '{$data['id']}'")->save($update_data);
		$sql_last =M("Admin_role")->getLastSql();
		if($status){
			write_action_log($sql_last,getModuleAction() , $this->username,get_client_ip(),1,"修改角色为{$data['rolename']}的权限成功");
			$this->role_id = $data['id'];
			$this->perm_data = $role_data;
			$this->make_cache();
			return array('status'=>1 , 'message'=>'success' );
		}
		write_action_log($sql_last,getModuleAction(),$this->username,get_client_ip(),0,"服务器繁忙请稍后");
		return array('status'=> 0  , 'message'=>'服务器繁忙请稍后' );
	
	}	
	//生成缓存
	private function make_cache(){
		if(!is_writable(C("role_cache_path"))){
			exit("目录".C("role_cache_path")."不可写");
		}
		$configfile = C("role_cache_path")."/cache_role_{$this->role_id}.inc.php";
		$str = '' ;
		$time = date("Y-m-d H:i:s",time());
		$fp = fopen($configfile,'w');
		flock($fp,3);
		fwrite($fp,"<"."?php\r\n");
		fwrite($fp,"/*团队角色缓存*/\r\n");
		fwrite($fp,"/*author speakphp.com*/\r\n");
		fwrite($fp,"/*time {$time}*/\r\n");
		$this->perm_data = delete_str_($this->perm_data);
		$str.="\$role_array = ";
		
		$str.= var_export($this->perm_data,true)  ;
		fwrite($fp,"{$str};\r\n");
		//fwrite($fp,");\r\n");
		fwrite($fp,"?".">");
		fclose($fp);
	}

	//查询有效的角色
	public function getRole(){
		return M("Admin_role")->where("status = 1 ")->select();
	}
	
}
